package cn.cs.yangtai.controller;

import cn.cs.yangtai.entity.Blog;
import cn.cs.yangtai.entity.Comment;
import cn.cs.yangtai.entity.vo.BlogVo;
import cn.cs.yangtai.entity.vo.CommentVo;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.entity.vo.ResponsePojo;
import cn.cs.yangtai.service.BlogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * (Blog)表控制层
 *
 * @author makejava
 * @since 2024-12-12 09:23:23
 */
@RestController
@CrossOrigin
@Tag(name = "话题")
@RequestMapping("blog")
public class BlogController {
    /**
     * 服务对象
     */
    @Resource
    private BlogService blogService;

    /**
     * 模糊查询话题数据
     * @param title 模糊查询的话题标题
     * @return 查询结果列表
     */
    @Operation(summary = "根据标题模糊查询话题")
    @GetMapping("/search")
    public ResponseEntity<List<BlogVo>> searchByTitle(@RequestParam("title") String title) {
        List<BlogVo> blogVos = blogService.queryByTitle(title);
        return ResponseEntity.ok(blogVos);
    }

    /**
     * 分页查询
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @Operation(summary = "话题分页")
    @GetMapping
    public ResponsePojo<PageResult<BlogVo>> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageResult<BlogVo> result=blogService.queryByPage(cuttentPage,pageSize);
        return ResponsePojo.success(result);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "根据id查询话题")
    @GetMapping("{id}")
    public ResponseEntity<BlogVo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.blogService.queryById(id));
    }

    /**
     * 根据用户查询话题数据
     * @return 对象列表
     */
    @Operation(summary = "查询用户发布的话题")
    @GetMapping("/blogs")
    public List<BlogVo> blogs(Integer userid) {
       return blogService.queryByUserid(userid);
    }
    /**
     * 新增数据
     *
     * @param
     * @return 新增结果
     */
    @Operation(summary = "添加话题")
    @PostMapping("/addBlog")
    public ResponseEntity<?> add(Integer blogUserid,String blogTitle,String blogContent) {
        Blog blog=new Blog();
        blog.setBlogUserid(blogUserid);
        blog.setBlogTitle(blogTitle);
        blog.setBlogContent(blogContent);
        blog.setBlogDate(new Date());
        blogService.insert(blog);
        return ResponseEntity.ok().body("话题发布成功");
    }

    /**
     * 编辑数据
     *
     * @return 编辑结果
     */
    @Operation(summary = "修改话题")
    @PutMapping("/editBlog")
    public ResponseEntity<?> edit(Integer blogId,Integer blogUserid,String blogTitle,String blogContent) {
        Blog existBlog=blogService.queryBy(blogId);
        if (!Objects.equals(blogUserid,existBlog.getBlogUserid())){
            return ResponseEntity.ok().body("您无权修改此话题");
        }
        Blog blog=new Blog();
        blog.setBlogId(blogId);
        blog.setBlogTitle(blogTitle);
        blog.setBlogContent(blogContent);
        blog.setBlogDate(new Date());
        BlogVo result=blogService.update(blog);
        if (result != null) {
            return ResponseEntity.ok().body("修改成功");
        } else {
            return ResponseEntity.ok().body("修改失败");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @Operation(summary = "根据话题id删除话题")
    @DeleteMapping
    public ResponseEntity<?> deleteById(Integer id,Integer blogUserid) {
        Blog blog=blogService.queryBy(id);
        if (!Objects.equals(blogUserid,blog.getBlogUserid())){
            return ResponseEntity.ok().body("您无权删除此话题");
        }
       Boolean result=blogService.deleteById(id);
        if (result != null) {
            return ResponseEntity.ok().body("删除成功");
        } else {
            return ResponseEntity.ok().body("删除失败");
        }
    }

}

